<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Exercise 5.39: SDP relaxations of the two-way partitioning problem</title>
<link rel="canonical" href="/Users/mcgrant/Projects/CVX/examples/cvxbook/Ch05_duality/html/ex_5_39.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Exercise 5.39: SDP relaxations of the two-way partitioning problem</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe. "Convex Optimization"</span>
<span class="comment">% Jo&euml;lle Skaf - 09/07/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% Compares the optimal values of:</span>
<span class="comment">% 1) the Lagrange dual of the two-way partitioning problem</span>
<span class="comment">%               maximize    -sum(nu)</span>
<span class="comment">%                   s.t.    W + diag(nu) &gt;= 0</span>
<span class="comment">% 2) the SDP relaxation of the two-way partitioning problem</span>
<span class="comment">%               minimize    trace(WX)</span>
<span class="comment">%                   s.t.    X &gt;= 0</span>
<span class="comment">%                           X_ii = 1</span>

<span class="comment">% Input data</span>
randn(<span class="string">'state'</span>,0);
n = 10;
W = randn(n); W = 0.5*(W + W');

<span class="comment">% Lagrange dual</span>
fprintf(1,<span class="string">'Solving the dual of the two-way partitioning problem...'</span>);

cvx_begin <span class="string">sdp</span>
    variable <span class="string">nu(n)</span>
    maximize ( -sum(nu) )
    W + diag(nu) &gt;= 0;
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);
opt1 = cvx_optval;

<span class="comment">% SDP relaxation</span>
fprintf(1,<span class="string">'Solving the SDP relaxation of the two-way partitioning problem...'</span>);

cvx_begin <span class="string">sdp</span>
    variable <span class="string">X(n,n)</span> <span class="string">symmetric</span>
    minimize ( trace(W*X) )
    diag(X) == 1;
    X &gt;= 0;
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);
opt2 = cvx_optval;

<span class="comment">% Displaying results</span>
disp(<span class="string">'------------------------------------------------------------------------'</span>);
disp(<span class="string">'The optimal value of the Lagrange dual and the SDP relaxation fo the    '</span>);
disp(<span class="string">'two-way partitioning problem are, respectively, '</span>);
disp([opt1 opt2])
disp(<span class="string">'They are equal as expected!'</span>);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Solving the dual of the two-way partitioning problem... 
Calling SDPT3 4.0: 55 variables, 10 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 10
 dim. of sdp    var  = 10,   num. of sdp  blk  =  1
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|6.8e+00|4.3e+00|1.0e+03| 0.000000e+00  0.000000e+00| 0:0:00| chol  1  1 
 1|1.000|1.000|2.6e-06|4.2e-02|8.6e+01|-7.238328e+00 -9.272163e+01| 0:0:00| chol  1  1 
 2|1.000|0.880|7.1e-08|8.7e-03|1.6e+01|-1.728912e+01 -3.312548e+01| 0:0:00| chol  1  1 
 3|0.884|1.000|1.6e-08|4.2e-04|4.7e+00|-2.624691e+01 -3.095297e+01| 0:0:00| chol  1  1 
 4|0.962|0.948|4.7e-09|6.1e-05|2.5e-01|-2.863288e+01 -2.888462e+01| 0:0:00| chol  1  1 
 5|0.979|0.989|4.8e-10|4.8e-06|1.9e-02|-2.881025e+01 -2.882921e+01| 0:0:00| chol  1  1 
 6|0.946|0.982|2.8e-10|5.0e-07|9.2e-04|-2.882492e+01 -2.882583e+01| 0:0:00| chol  1  1 
 7|1.000|1.000|1.9e-09|5.6e-11|8.9e-05|-2.882561e+01 -2.882569e+01| 0:0:00| chol  1  1 
 8|0.974|0.984|1.6e-10|8.6e-11|2.2e-06|-2.882567e+01 -2.882568e+01| 0:0:00| chol  1  1 
 9|1.000|1.000|5.4e-11|3.1e-11|1.4e-07|-2.882567e+01 -2.882568e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   =  9
 primal objective value = -2.88256749e+01
 dual   objective value = -2.88256750e+01
 gap := trace(XZ)       = 1.41e-07
 relative gap           = 2.40e-09
 actual relative gap    = 2.35e-09
 rel. primal infeas (scaled problem)   = 5.44e-11
 rel. dual     "        "       "      = 3.10e-11
 rel. primal infeas (unscaled problem) = 0.00e+00
 rel. dual     "        "       "      = 0.00e+00
 norm(X), norm(y), norm(Z) = 8.6e+00, 1.0e+01, 1.2e+01
 norm(A), norm(b), norm(C) = 4.2e+00, 4.2e+00, 7.6e+00
 Total CPU time (secs)  = 0.11  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 1.1e-10  0.0e+00  9.7e-11  0.0e+00  2.4e-09  2.4e-09
-------------------------------------------------------------------
 
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -26.6924
 
Done! 
Solving the SDP relaxation of the two-way partitioning problem... 
Calling SDPT3 4.0: 55 variables, 10 equality constraints
------------------------------------------------------------

 num. of constraints = 10
 dim. of sdp    var  = 10,   num. of sdp  blk  =  1
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|6.8e+00|4.3e+00|1.0e+03| 0.000000e+00  0.000000e+00| 0:0:00| chol  1  1 
 1|1.000|1.000|2.6e-06|4.2e-02|8.6e+01|-7.238328e+00 -9.272163e+01| 0:0:00| chol  1  1 
 2|1.000|0.880|7.1e-08|8.7e-03|1.6e+01|-1.728912e+01 -3.312548e+01| 0:0:00| chol  1  1 
 3|0.884|1.000|1.6e-08|4.2e-04|4.7e+00|-2.624691e+01 -3.095297e+01| 0:0:00| chol  1  1 
 4|0.962|0.948|4.7e-09|6.1e-05|2.5e-01|-2.863288e+01 -2.888462e+01| 0:0:00| chol  1  1 
 5|0.979|0.989|4.8e-10|4.8e-06|1.9e-02|-2.881025e+01 -2.882921e+01| 0:0:00| chol  1  1 
 6|0.946|0.982|2.8e-10|5.0e-07|9.2e-04|-2.882492e+01 -2.882583e+01| 0:0:00| chol  1  1 
 7|1.000|1.000|1.9e-09|5.6e-11|8.9e-05|-2.882561e+01 -2.882569e+01| 0:0:00| chol  1  1 
 8|0.974|0.984|1.6e-10|8.6e-11|2.2e-06|-2.882567e+01 -2.882568e+01| 0:0:00| chol  1  1 
 9|1.000|1.000|5.4e-11|3.1e-11|1.4e-07|-2.882567e+01 -2.882568e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   =  9
 primal objective value = -2.88256749e+01
 dual   objective value = -2.88256750e+01
 gap := trace(XZ)       = 1.41e-07
 relative gap           = 2.40e-09
 actual relative gap    = 2.35e-09
 rel. primal infeas (scaled problem)   = 5.44e-11
 rel. dual     "        "       "      = 3.10e-11
 rel. primal infeas (unscaled problem) = 0.00e+00
 rel. dual     "        "       "      = 0.00e+00
 norm(X), norm(y), norm(Z) = 8.6e+00, 1.0e+01, 1.2e+01
 norm(A), norm(b), norm(C) = 4.2e+00, 4.2e+00, 7.6e+00
 Total CPU time (secs)  = 0.09  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 1.1e-10  0.0e+00  9.7e-11  0.0e+00  2.4e-09  2.4e-09
-------------------------------------------------------------------
 
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -26.6924
 
Done! 
------------------------------------------------------------------------
The optimal value of the Lagrange dual and the SDP relaxation fo the    
two-way partitioning problem are, respectively, 
  -26.6924  -26.6924

They are equal as expected!
</pre>
</div>
</body>
</html>